$(function(){
	var pageIndex = 1;
	var deleteNode;
	var changeId = null;
	var add = 0;

	//获取教师信息
	function requestTeacherS(data){
		if( data.status == true ){
			if( data.status == true ){

				var length = $(".saler-list").length;

				for( var j = 0; j < length ; j++){
					$($(".saler-list")[j]).html("");
					var str = '<option  class = "teacher" value ="0"></option>';
					for( var i = 0; i< data.data.content.length; i++ ){
						var item = data.data.content[i];
						str += '<option  class = ""'+'subject="'+item.subject+'"value ="'+item.id+'">'+item.name+'</option>';
					}
					$($(".saler-list")[j]).html(str);
				}
				
				
			}

		}else{
			alert("获取教师失败");
		}
	}
	function requestTeacherE(data){
			alert("获取教师失败Error");
	}

	

	//获取课程信息
	function requestClassS(data){
		if( data.status == true ){
			console.log(data);
			// if( data.keeperList.length == 0 ){
			// 	return;
			// }
			// for( var i = 0; i< data.keeperList.length; i++ ){
			// 	var item = data.keeperList[i];
			// 	var node = $(".detail-check").find("tr").eq(0).clone();
			// 	node.attr("id",item.id);
			// 	node.find(".name").html(item.name);
			// 	node.find(".account").html(item.account);
			// 	node.find(".phone").html(item.phone);
			// 	node.find(".idNumber").html(item.idNumber);
			// 	node.find(".jobNumber").html(item.jobNumber);
			// 	node.removeClass("hide");
			// 	$(".detail-check").append(node);
			// 	//密码
			// 	// node.find(".password").html(data.keeperList.password);
			// }
		}
	}

	function requestClassS(data){
		alert("获取课程信息失败 error");
	}
	
	//提交课程
	function postClassS(data){
		console.log(data);
		if( data.status == true ){
			location.href = "class-info.html"
			// console.log(data);
		}else{
			alert(data.msg+"提交失败");
		}
	}
	function postClassE(data){
		alert("提交失败 error");
	}

	//--提交课程
	$(".post-class").click(function(){
		var classArr = [];
		var classLength = $(".child-record").length;
		
		for ( var i = 0; i < classLength; i++){
			var startTime = $(".startTime")[i].innerHTML;
			var endTime = $(".endTime")[i].innerHTML;
			var teacherArr = [];
			for( var j = 0 ; j < 5 ; j++ ){
				var node = $( $(".child-record")[i] ).find(".saler-list option:selected")[j];
				teacherArr[j] = $(node).val();
			}
			classArr[i]={
				"startTime":startTime,
				"endTime":endTime,
				"teacher":teacherArr
			}
		}
		console.log(classArr);
		
		Ajax({
			url: "http://106.75.143.27/classSchedules/createAll",
			type: "post",
			data: JSON.stringify(classArr),
            dataType: 'json',
            contentType:'application/json',
			call1: postClassS,
			call2: postClassE
		});
	});

	//--添加课程
	$(".add-class").click(function(){
		var node = $(".detail-check").find("tr").eq(0).clone();
		$(".detail-check").append(node);
	});

	//--删除课程
	$(".detail-check").on("select",".delete",function(){
		var node = $(this).parent().parent();
		console.log();
		node.remove();
	});

	//--选中教师
	$(".detail-check").on("click",".saler-list",function(){
		var subject = $(this).find("option:selected").attr("subject");
		$(this).parent().prev().find(".subject").html(subject);
	});
	

	//加载教师记录
	$.ajax({
		type: "get",
		url: 'http://106.75.143.27/teachers',
		data: {"page":0},
		contentType: 'application/x-www-form-urlencoded',
		success: requestTeacherS,
		error: requestTeacherE
	});

	//加载课程表
	// $.ajax({
	// 	type: "get",
	// 	url: 'http://106.75.143.27/classSchedules',
	// 	// data: {"page":0},
	// 	contentType: 'application/x-www-form-urlencoded',
	// 	success: requestClassS,
	// 	error: requestClassE
	// });
})